package com.love.mire;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.graphics.Paint.Style;
import android.graphics.RectF;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup.LayoutParams;

import java.util.ArrayList;
import java.util.List;

/**
 * 柱状图
 *
 * @author Administrator
 */
public class HomeColumnar extends View {

    private OnInsideTouchListener listener;
    private List<Score> score;
    private float tb;
    private float interval_left_right;
    private Paint paint_date;//底部画笔
    private Paint paint_rectf_gray;//边框画笔
    private Paint paint_rectf_blue;//数据画笔
    private boolean inside = false;

    private int fineLineColor = 0x5faaaaaa; // 灰色
    private int blueLineColor = 0xff00ffff; // 蓝色
    private float downX;
    private float downY;
    private int selectPosition;
    private Canvas Hightcanvas;
    // 每组数据对应的区域
    private ArrayList<RectF> rects;

    public HomeColumnar(Context context, List<Score> score) {
        super(context);
        init(score);
    }

    public void init(List<Score> score) {
        if (null == score || score.size() == 0)
            return;
        this.score = score;
        Resources res = getResources();
        tb = res.getDimension(R.dimen.historyscore_tb);
        interval_left_right = tb * 5.0f;

        paint_date = new Paint();
        paint_date.setStrokeWidth(tb * 0.1f);
        paint_date.setTextSize(res.getDimension(R.dimen.text_size_16));
        paint_date.setColor(fineLineColor);
        paint_date.setTextAlign(Align.CENTER);

        paint_rectf_gray = new Paint();
        paint_rectf_gray.setStrokeWidth(tb * 0.1f);
        paint_rectf_gray.setColor(fineLineColor);
        paint_rectf_gray.setStyle(Style.FILL);
        paint_rectf_gray.setAntiAlias(true);

        paint_rectf_blue = new Paint();
        paint_rectf_blue.setStrokeWidth(tb * 0.1f);
        paint_rectf_blue.setColor(Color.parseColor("#a3e6ff"));
        paint_rectf_blue.setStyle(Style.FILL);
        paint_rectf_blue.setAntiAlias(true);

        setLayoutParams(new LayoutParams(
                (int) (this.score.size() * interval_left_right),
                LayoutParams.MATCH_PARENT));
    }

    protected void onDraw(Canvas c) {
        if (null == score || score.size() == 0)
            return;
        this.Hightcanvas = c;
        drawDate(c);
        drawRectf(c);
    }

    /**
     * 绘制矩形
     *
     * @param c
     */
    public void drawRectf(Canvas c) {
        rects = new ArrayList<>();
        rects.clear();

        for (int i = 0; i < score.size(); i++) {
            /*RectF f = new RectF();
            f.set(tb * 0.2f + interval_left_right * i,
					getHeight() - tb * 11.0f, tb * 3.2f + interval_left_right
							* i, getHeight() - tb * 2.0f);
			c.drawRoundRect(f, tb * 0.3f, tb * 0.3f, paint_rectf_gray);*/

            float base = score.get(i).score * (tb * 10.0f / 100);
            RectF f1 = new RectF();
            f1.set(tb * 0.2f + interval_left_right * i, getHeight() - (base + tb * 1.5f), tb * 3.2f + interval_left_right * i, getHeight() - tb * 1.5f);
            c.drawRoundRect(f1, tb * 0.3f, tb * 0.3f, paint_rectf_blue);
            rects.add(f1);
        }
    }

    /**
     * 绘制日期
     *
     * @param c
     */
    public void drawDate(Canvas c) {
        for (int i = 0; i < score.size(); i++) {
            String date = score.get(i).date;
            String date_1 = date
                    .substring(date.indexOf("-") + 1, date.length());
            c.drawText(date_1, tb * 1.7f + interval_left_right * i,
                    getHeight(), paint_date);

        }
    }


    /*private boolean isInside(float downX, float downY) {

        for (int i = 0; i < score.size(); i++) {

            selectPosition = i;
            return true;
        }
        return false;
    }*/

    private boolean getPosition(float x12, float y12) {
        if (null == rects) {
            return false;
        }
        for (int i = 0; i < rects.size(); i++) {
            RectF rectF = rects.get(i);
            if (rectF.contains(x12, y12)) {

                selectPosition = i;
                return true;
            }
        }
        return false;
    }

    ;


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                break;
            case MotionEvent.ACTION_UP:
                downX = event.getX();
                downY = event.getY();
                inside = getPosition(downX, downY);
                if (inside) {
                    listener.show(score.get(selectPosition));
                } else {
                    return false;
                }
                break;
        }
        return true;
    }

    public interface OnInsideTouchListener {
        void show(Score score);
    }

    public void setListener(OnInsideTouchListener listener) {
        this.listener = listener;
    }
}
